package com.jsz.peini.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;

public class AppPushIOS {
	private static final Logger logger = LoggerFactory.getLogger(AppPushIOS.class);
	private static String appId = "wZEa9Xszk66U4zj0hkDQrA";
    private static String appKey = "vMnO5zNQro5A0xxbxUCGR7";
    private static String masterSecret = "hAFAgLLxh56PfdNKO5daU2";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    
    public static String sendPush(JSONObject smsObject,String [] aliasList){
    	IGtPush push = new IGtPush(url, appKey, masterSecret);
        
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        ListMessage message = new ListMessage();
        message.setData(getTemplate(smsObject));
        // 设置消息离线，并设置离线时间
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        
        // 配置推送目标
        List<Target> targets = new ArrayList<Target>();
        for (String alias : aliasList) {
        	Target target = new Target();
        	target.setAppId(appId);
        	target.setAlias(alias);
        	//target.setClientId(alias);
        	targets.add(target);
		}
        // taskId用于在推送时去查找对应的message
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, targets);
        
        String reStr = ret.getResponse().toString();
        return reStr;
    }

    public static void main(String[] args) throws IOException {

        IGtPush push = new IGtPush(url, appKey, masterSecret);
        
        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        /*AppMessage message = new AppMessage();
        message.setData(getTemplate());
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);
        
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId("6c9064c433ffc5211632b5003b62b919");
        
        IPushResult ret = push.pushMessageToApp(message);
        
        
    	
    	/*IGtPush push = new IGtPush(url, appKey, masterSecret);
    	TransmissionTemplate template = getTemplate();
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0); 
        Target target = new Target();
        target.setAppId(appId);
        //target.setClientId("6c9064c433ffc5211632b5003b62b919");
        target.setAlias("金手指");
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            logger.error(e.getMessage(),e);
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        */
    }
    
    public static NotificationTemplate getNotifyTemplate() {
    	JSONObject object = new JSONObject();
        object.put("title", "透传消息");
        object.put("content", "发送给亚坤的透传消息");
        object.put("type", 1);
        object.put("paramId", "1050");
        
    	 NotificationTemplate template = new NotificationTemplate();
         template.setAppId(appId);
         template.setAppkey(appKey);
         template.setTitle("系统消息");
         template.setText("发送给亚坤");
         template.setTransmissionContent(object.toString());
	    /*TransmissionTemplate template = new TransmissionTemplate();
	    template.setAppId(appId);
	    template.setAppkey(appKey);*/
	    /*template.setTransmissionContent("透传内容");
	    template.setTransmissionType(2);
	    APNPayload payload = new APNPayload();
	    payload.setAutoBadge("+1");
	    payload.setContentAvailable(1);
	    payload.setSound("default");
	    payload.setCategory("$由客户端定义");
	    //简单模式APNPayload.SimpleMsg 
	    payload.setAlertMsg(getDictionaryAlertMsg(object));
	    //字典模式使用下者
	    //payload.setAlertMsg(getDictionaryAlertMsg());
	    template.setAPNInfo(payload);*/
	    return template;
	}
    
    
    public static TransmissionTemplate getTemplate(JSONObject object) {
    	/*JSONObject object = new JSONObject();
        object.put("title", "透传消息");
        object.put("content", "发送给亚坤的透传消息");
        object.put("type", 1);
        object.put("paramId", "1050");*/
    	
	    TransmissionTemplate template = new TransmissionTemplate();
	    template.setAppId(appId);
	    template.setAppkey(appKey);
	    template.setTransmissionContent(object.toString());
	    template.setTransmissionType(2);//强制启动应用；2等待应用启动
	    APNPayload payload = new APNPayload();
	    payload.setAutoBadge("+1");
	    payload.setContentAvailable(1);
	    payload.setSound("default");
	    payload.setCategory("$由客户端定义");
	    //简单模式APNPayload.SimpleMsg 
	    payload.setAlertMsg(getDictionaryAlertMsg(object));
	    //payload.setAlertMsg(new APNPayload.SimpleAlertMsg(object.getString("content")));
	    //字典模式使用下者
	    //payload.setAlertMsg(getDictionaryAlertMsg());
	    template.setAPNInfo(payload);
	    return template;
	}
	private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(JSONObject object){
		APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
	    alertMsg.setBody(object.getString("content"));
	    alertMsg.setActionLocKey("ActionLockey");
	    alertMsg.setLocKey(object.getString("content"));
	    alertMsg.addLocArg("loc-args");
	    alertMsg.setLaunchImage("launch-image");
	    // IOS8.2以上版本支持
	    alertMsg.setTitle(object.getString("title"));
	    alertMsg.setTitleLocKey(object.getString("title"));
	    alertMsg.addTitleLocArg("TitleLocArg");
	    return alertMsg;
	}
}
